package org.jeecg.modules.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.jd.entity.JD_goods;
import org.jeecg.modules.jd.entity.JD_goods_category;
import org.jeecg.modules.jd.entity.JD_position;
import org.jeecg.modules.jd.mapper.JD_goods_categoryMapper;
import org.jeecg.modules.jd.service.IJD_goods_categoryService;
import org.jeecg.modules.jd.service.IJD_positionService;
import org.jeecg.modules.jd.util.GoodCategoryUtil;
import org.jeecg.modules.jd.util.JDGoodsRequest;
import org.jeecg.modules.jd.util.JDJingFenGoodRequest;
import org.jeecg.modules.jd.util.JDGoodsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * @Description: 京东类目
 * @Author: jeecg-boot
 * @Date: 2020-06-10
 * @Version: V1.0
 */
@Service
public class JD_goods_categoryServiceImpl extends ServiceImpl<JD_goods_categoryMapper, JD_goods_category> implements IJD_goods_categoryService {

    @Autowired
    @Lazy(true)
    private IJD_positionService jD_positionService;

    @Override
    public void initData() {

        String json = GoodCategoryUtil.getCategoryData();
        JSONObject jsonObject = JSON.parseObject(json);
        String contentlist = jsonObject.getString("data");
        List<JD_goods_category> jd_goods_categoryList = JSON.parseArray(contentlist, JD_goods_category.class);
        if (null != jd_goods_categoryList && jd_goods_categoryList.size() > 0) {

            for (JD_goods_category jd_goods_category : jd_goods_categoryList) {
                JD_goods_category old_jd_goods_cat = this.getById(jd_goods_category.getId());
                if (null != old_jd_goods_cat) {
                    this.updateById(jd_goods_category);
                } else {
                    this.save(jd_goods_category);
                }
            }
        }
    }

    @Override
    public Result<?> getGoodsCatgorys() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        return Result.ok(list(queryWrapper));
    }

    @Override
    public Result<?> getJingfenGoodsData(JDJingFenGoodRequest jdGoodRequest) {

        if (null == jdGoodRequest.getUser_id()) {
            return Result.error("用户信息没有");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", jdGoodRequest.getUser_id());
        JD_position jd_position = jD_positionService.getOne(queryWrapper);
        jdGoodRequest.setJd_position_id(jd_position.getId());
        Map<String, Object> jdGoodsList = JDGoodsUtil.getJingfenGoods(jdGoodRequest);
        return Result.ok(jdGoodsList);

    }

    @Override
    public Result<?> getGoodsData(JDGoodsRequest jdGoodsRequest) {
        if (null == jdGoodsRequest.getUser_id()) {
            return Result.error("用户信息没有");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", jdGoodsRequest.getUser_id());
        JD_position jd_position = jD_positionService.getOne(queryWrapper);
        jdGoodsRequest.setJd_position_id(jd_position.getId());
        Map<String, Object> jdGoodsList = JDGoodsUtil.getGoods(jdGoodsRequest);
        return Result.ok(jdGoodsList);
    }
}
